package com.lkh.map;

import com.lkh.common.IKUtil;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.types.Row;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

public class WordCountFlatMapFuncation implements FlatMapFunction<Row, Tuple3<String, String, Integer>> {
    @Override
    //重写flatmap方法
    public void flatMap(Row value, Collector<Tuple3<String, String, Integer>> out) throws Exception {
        //获取舆情ID
        String sentId = (String) value.getField(0);
        //获取舆情内容
        String text = (String) value.getField(1);

        //将内容进行分词
        ArrayList<String> words = IKUtil.fit(text);
        //遍历分词后的集合
        for (String word : words) {
            //返回一个三元组（舆情编号，词，1）
            Tuple3<String, String, Integer> result = new Tuple3<>(sentId, word, 1);
            out.collect(result);
        }
    }
}
